package demo2;

public class MySingleList implements IList{
    static class ListNode {
        public int val;//数值
        public ListNode next;//下一个节点地址
        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head = null;

    /**
     * 自己创建数据
     * @param data
     */
    public void create() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = null;
        head = node1;
    }
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);

        node.next = this.head;
        this.head = node;
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);

        if(this.head == null) {
            this.head = node;
        }else {
            ListNode cur = head;
            while(cur.next != null) {
                cur = cur.next;
            }

            cur.next = node;
        }
    }

    @Override
    public void addIndex(int index, int data) {
        if(head == null) {
            return;
        }
        ListNode node = new ListNode(data);
        ListNode cur = findIndexListNode(index);
        node.next = cur.next;
        cur.next = node;
    }
    private ListNode findIndexListNode(int index) {
        if(index < 0 || index > size()) {
            throw new IndexException("下标不合法");
        }

        ListNode cur = head;
        int count = 0;
        while(count < index) {
            count++;
            cur = cur.next;
        }
        return cur;
    }
    @Override
    public boolean contains(int key) {
        if(head == null) {
            return false;
        }

        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }

        return false;
    }

    @Override
    public void remove(int key) {
        if(head == null) {
            return;
        }

        if(head.val == key) {
            head = head.next;
        }
        ListNode cur = findPrevKey(key);
        if(cur == null) {
            return;
        }
        cur.next = cur.next.next;
    }
    private ListNode findPrevKey(int key) {
        ListNode cur = head;
        while(cur.next != null) {
            if(cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    @Override
    public void removeAllKey(int key) {
        if(head == null) {
            return;
        }

        while(head.val == key) {
            head = head.next;
        }

        ListNode cur = head;
        ListNode prev = head;
        while(cur != null) {
            if(cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
    }

    @Override
    public int size() {
        if(this.head == null) {
            return 0;
        }
        int count = 0;
        ListNode cur = this.head;

        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public void clear() {
        this.head = null;
    }

    @Override
    public void display() {
        if(head == null) {
            return;
        }

        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
}
